package cn.lxh.template;

import cn.lxh.constants.Constants;
import cn.lxh.constants.ErrorCode;
import cn.lxh.pojo.bo.AccessTokenBo;
import cn.lxh.pojo.bo.OpenIdBo;
import cn.lxh.pojo.bo.PhoneNumberBo;
import cn.lxh.properties.WxMinProperties;
import cn.lxh.utils.AssertUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lxh Create Time: 2023/12/23 10:58
 */
@Slf4j
public class WeChatTemplate {

    private WxMinProperties wxMinProperties;

    private RestTemplate restTemplate;

    public WeChatTemplate(WxMinProperties wxMinProperties, RestTemplate restTemplate) {
        this.wxMinProperties = wxMinProperties;
        this.restTemplate = restTemplate;
    }

    // 获取openId
/*    public String testGetOpenId(){
        return "666";
    }*/

    // 获取微信openId
    public OpenIdBo getOpenId(String wxCode) {
        // 获取openId的请求地址
        String url = String.format(Constants.URL.OPEN_ID_URL,
                wxMinProperties.getAppid(),
                wxMinProperties.getSecret(),
                wxCode);
        // 通过Http通信发送get请求
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        // 输出打印responseEntity
        log.info("获取微信openId的结果{} - {}", responseEntity.getBody(), responseEntity.getStatusCode());
        // 判断状态解码 (不是200成功状态码就微信注册失败)
        AssertUtil.isEquals(responseEntity.getStatusCodeValue(), HttpStatus.OK.value(), ErrorCode.PARAM_OPENID_ERROR.getMessage());
        // 将responseEntity (JSON格式字符串)转为java对象
        OpenIdBo openIdBo = JSON.parseObject(responseEntity.getBody(), OpenIdBo.class);
        return openIdBo;
    }

    // 获取accessToken
    public AccessTokenBo getAccessToken() {
        // 获取accessToken的请求地址
        String url = String.format(Constants.URL.ACCESS_TOKEN_URL,
                wxMinProperties.getAppid(),
                wxMinProperties.getSecret());
        // 通过Http通信发送get请求
        ResponseEntity<AccessTokenBo> responseEntity = restTemplate.getForEntity(url, AccessTokenBo.class);
        AssertUtil.isEquals(responseEntity.getStatusCodeValue(), HttpStatus.OK.value(), "accessToken获取失败");
        return responseEntity.getBody();
    }

    // 获取手机号
    public PhoneNumberBo getPhoneNumber(String phoneCode){
        // 判断传入的phoneCode是否为空
        AssertUtil.isNotEmpty(phoneCode, "手机号凭证code为空");

        // 获取accessToken
        AccessTokenBo accessTokenBo = getAccessToken();
        String accessToken = accessTokenBo.getAccess_token();
        log.info("获取的accessToken: {}", accessToken);

        // 通过phoneCode和accessToken获取手机号的请求地址
        String url = String.format(Constants.URL.PHONE_NUMBER_URL, accessToken);
        // 创建请求头对象
        HttpHeaders headers = new HttpHeaders();
        // 设置请求头格式 (JSON格式)
        headers.setContentType(MediaType.APPLICATION_JSON);
        // 创建请求参数
        Map<String, String> params = new HashMap<>();
        params.put("code", phoneCode);
        HttpEntity<Map> entity = new HttpEntity<>(params, headers);
        ResponseEntity<PhoneNumberBo> responseEntity = restTemplate.postForEntity(url, entity, PhoneNumberBo.class);
        return responseEntity.getBody();
    }
}
